home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5390 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  94 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Sorting pointers to objects with qsort
  5. Message-ID: <marnoldDM8Mw7.A1y@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <4f18c0$12do@bigblue.oit.unc.edu>
  8. Date: Sun, 4 Feb 1996 06:19:18 GMT
  9. Sender: marnold@netcom4.netcom.com
  10.  
  11. mebust@email.unc.edu (Scott Mebust) writes:
  12.  
  13. >I'd appreciate any help on the following problem:  sorting pointers to 
  14. >objects (based on object attributes) using qsort.
  15.  
  16. >If replying, please send an e-mail as well.  Thanks.
  17.  
  18. >Scott
  19. >mebust@email.unc.edu
  20.  
  21.  
  22. >// QUESTION:  Why does the following code return the wrong results?
  23.  
  24. >// I can't seem to figure this one out.  The program below attempts to
  25. >// sort an array of pointers to String objects using qsort.  It compiles
  26. >// and may *seem* to return the correct results but does not actually
  27. >// sort the pointers based on any valid data.
  28.  
  29. >// I believe the problem has to do with "qsort" and "const" objects.  It
  30. >// appears as though qsort is creating new, const String(s) but the
  31. >// str_ member is not copying correctly.
  32.  
  33. No, this is a guess and it is way off.  Your problem is much more basic.
  34. See below.
  35.  
  36. >// This is probably some *elementary* problem but I just can't *see* what
  37. >// is going wrong.
  38.  
  39. You are right.
  40.  
  41. >int StrCmpFn (const void* VP1, const void* VP2)
  42. >{
  43. >  return strcmp(((const String*)VP1)->GetValue(),
  44. >    ((const String*)VP2)->GetValue());
  45. >}
  46.  
  47. You're mistake is here.  StrCmpFn is passed pointers to the array
  48. elements that qsort is sorting.  In your case, the array elements
  49. themselves are pointers!  So, StrCmpFn is being passed pointers to
  50. pointers to String objects, NOT pointers to String objects, which
  51. is how you are treating them.  Change StrCmpFn to take into account
  52. the *two* levels of indirection and things should work much better.
  53.  
  54. int StrCmpFn (const void* VP1, const void* VP2)
  55. {
  56.   const String* ps1 = *(const String**)VP1;
  57.   const String* ps2 = *(const String**)VP2;
  58.   
  59.   return strcmp(ps1->GetValue(), ps2->GetValue());
  60. }
  61.  
  62. >void main (void)
  63. >{
  64. >  String A("Apple"), B("Baseball"), C("Chevrolet");
  65. >  String* SPA[3] = {&C, &B, &A};
  66.  
  67. >  cout << *SPA[0] << endl
  68. >    << *SPA[1] << endl
  69. >    << *SPA[2] << endl << endl;
  70.  
  71. >  qsort(SPA,3,sizeof(SPA[0]),StrCmpFn);
  72.  
  73. >  cout << *SPA[0] << endl
  74. >    << *SPA[1] << endl
  75. >    << *SPA[2] << endl << endl;
  76. >}
  77.  
  78.  
  79. Don't feel bad.  This is a classic mistake made when learning how to
  80. use qsort().
  81.  
  82. Regards,
  83. -------------------------------------------------------------------------
  84. Matt Arnold                       |        | ||| | |||| |  | | || ||
  85. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  86. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  87. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  88. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  89. -------------------------------------------------------------------------
  90.  
  91.  
  92.  
  93.  
  94.